package com.wzy.vx.service.impl;

import com.wzy.vx.DTO.OrderDTO;
import com.wzy.vx.config.WechatUserConfig;
import com.wzy.vx.service.PushMsg;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @ProjectName: vx
 * @Package: com.wzy.vx.service.impl
 * @ClassName: PushMsgImpl
 * @Author: Administrator
 * @Description: ${description}
 * @Date: 2019/3/25 17:05
 * @Version: 1.0
 */
@Service
public class PushMsgImpl implements PushMsg {
    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private WechatUserConfig wechatUserConfig;

    private  final  static Logger log = LoggerFactory.getLogger(PushMsgImpl.class);

    @Override
    public void orderStatus(OrderDTO orderDTO) {
        WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
        //模板
        wxMpTemplateMessage.setTemplateId(wechatUserConfig.getTemplateId().get("orderStatus"));
        //接受者openId
        wxMpTemplateMessage.setToUser(orderDTO.getBuyerOpenid());
        //返回微信的内容
        List<WxMpTemplateData> data = Arrays.asList(
                new WxMpTemplateData("first", "小果果，请记得收货。"),
                new WxMpTemplateData("keyword1", "微信点餐"),
                new WxMpTemplateData("keyword2", "541881748"),
                new WxMpTemplateData("keyword3", orderDTO.getOrderId()),
                new WxMpTemplateData("keyword4", orderDTO.getOrderStatusEnum().getMsg()),
                new WxMpTemplateData("keyword5", "￥" + orderDTO.getOrderAmount()),
                new WxMpTemplateData("remark", "小哥哥,再来哦！")
        );
        //数据
        wxMpTemplateMessage.setData(data);
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
        } catch (WxErrorException e) {
            log.error("[微信模版消息]发送失败,tempmsg={}",e);
        }
    }
}
